<?php
/**
 * Manage categories for exchanges / offers / asks
 *
 * @package    letsManager
 * @subpackage letsCategory
 * @author     Jerome Fouilloy
 * @version    SVN: $Rev$
 */
class letsCategoryActions extends sfActions
{
  /**
  * Form new service
  */
  public function executeNew(sfWebRequest $request)
  {
	$this->form = new LetsMainCategoryForm(null,array('user' => $this->getUser()));
    $this->form->setDefault('association_id', $this->getUser()->getAssociationId());
  }
  /**
  * Creating new service
  */
  public function executeCreate(sfWebRequest $request)
  {
	  $this->form = new LetsMainCategoryForm(null,array('user' => $this->getUser()));
 //  $this->form->setDefault('association_id', $this->getUser()->getAssociationId());
	  $this->processForm($request, $this->form);
	  $this->setTemplate('new');
  }
  
  public function executeEdit(sfWebRequest $request)
  {
  	  $id = $request->getParameter('id');
  	  $this->forward404Unless($category = LetsMainCategoryTable::getInstance()->find($id));
	  $this->form = new LetsMainCategoryForm($category,array('user' => $this->getUser()));
	  if ($category->getAssociationId() != $this->getUser()->getAssociationId())
      {
        $this->redirect('@error_credentials');
      }
  }
  
  public function executeUpdate(sfWebRequest $request)
  {
      $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
  	  $id = $request->getParameter('id');
  	  $this->forward404Unless($service = LetsMainCategoryTable::getInstance()->find($id));
	  $this->form = new LetsMainCategoryForm($service,array('user' => $this->getUser()));
	  $this->processForm($request, $this->form);
	  $this->setTemplate('edit');
  }
  
  public function executeIndex(sfWebRequest $request)
  {
  	$user = $this->getUser();
  	$seePrivateCategories = $user->hasCredential(LetsManagerRights::USE_PRIVATE_CATEGORY);
	$this->categories = LetsMainCategoryTable::getAllCategories($user,true );
  }
  
  protected function processForm(sfWebRequest $request, sfForm $form)
  {

	  $form->bind(
	    $request->getParameter($form->getName())
	  );
	 
	  if ($form->isValid())
	  {

	    $category = $form->save();
			$this->redirect('@lets_category');
			 
	  }
  }
}